<?php

namespace app\filemanage\controller;

use think\Controller;
use think\Request;
use wenbin\filesystem\FileSystem;

class File extends Common
{
    private $_size = 1638400; //200KB
    private $_image = [
        'bmp'  => 'image/x-ms-bmp',
        'jpg'  => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'gif'  => 'image/gif',
        'png'  => 'image/png',
        // 'tif'  => 'image/tiff',
        // 'tiff' => 'image/tiff',
        // 'tga'  => 'image/x-targa',
        // 'psd'  => 'image/vnd.adobe.photoshop'
    ];
    private $_text = [
        'txt'  => 'text/plain',
        'php'  => 'text/x-php',
        'html' => 'text/html',
        'htm'  => 'text/html',
        'js'  => 'text/javascript',
        'css'  => 'text/css',
        'rtf'  => 'text/rtf',
        'rtfd' => 'text/rtfd',
        'py'  => 'text/x-python',
        'java' => 'text/x-java-source',
        'rb'  => 'text/x-ruby',
        'sh'  => 'text/x-shellscript',
        'pl'  => 'text/x-perl',
        'sql'  => 'text/x-sql'
    ];


    public function create() {
        if (Request::instance()->isPost()) {
            $path = USER_ROOT_PATH . input('post.pos');
            $filename = input('post.filename');
            $file = $path . $filename;

            if (!is_file($file)) {
                if (touch($file)) {
                    chmod($file, 0776);
                    return $this->success('文件创建成功');
                } else {
                    return $this->error('文件创建失败');
                }
            } else {
                return $this->error('文件已存在');
            }
        } else {
            // return $this->fetch('error');
        }
    }

    public function mkdir() {

        if (Request::instance()->isPost()) {
            $path = USER_ROOT_PATH . input('post.pos');
            $dirname = input('post.filename');
            $dir = $path . $dirname;
            if (!is_dir($dir)) {
                if (mkdir($dir)) {
                    return $this->success('文件夹创建成功');
                } else {
                    return $this->error('文件创建夹失败');
                }
            } else {
                return $this->error('文件已存在');
            }
        } else {
            // return $this->fetch('error');
        }
    }

    public function upload() {
        $file = request()->file('file');
        $pos = input('post.pos');
        // $cover = input('post.cover');
        // $file->getFilename();
        // return $this->error($filename);
        // if ($cover) {
        //     if (is_file(filename)) {
        //         # code...
        //     }
        // }
        $info = $file->move(USER_ROOT_PATH . $pos, '');
        if ($info) {
            return $this->success('文件上传成功');
        } else {
            return $this->error('文件上传失败');
        }
    }

    public function delete() {
        $path = input('post.path');
        if (!$path) {
            return $this->error('非法操作');
        }
        $src = USER_ROOT_PATH . $path;
        if (file_exists($src)) {
            if (FileSystem::delete($src)) {
                return $this->success('文件删除成功');
            } else {
                return $this->error('文件删除失败');
            }
        } else {
            return $this->error('文件不存在');
        }
    }


    public function download() {
        $path = input('get.path');
        $src = USER_ROOT_PATH . $path;
        if (is_file($src)) {
            $filename = basename($src);
            $filesize = filesize($src);
            header("Content-Type: application/octet-stream");
            header("Content-disposition: attachment; filename=" . $filename);
            header("Accept-length:" . $filesize);
            readfile($src);
            exit(); //###此行代码针对safari自动添加后缀问题
        } else {
            return $this->error('文件不存在');
        }
    }

    public function view() {
        if (Request::instance()->isPost()) {
            $path = input('post.path');
            $filename = USER_ROOT_PATH . $path;
            if (is_file($filename)) { 
                $finfo = finfo_open(FILEINFO_MIME);
                $mimetype = finfo_file($finfo, $filename);
                finfo_close($finfo);
                $mimetype = substr($mimetype, 0,  strpos($mimetype, ';'));
                if (in_array($mimetype, $this->_image)) {
                    return $this->success(3, '', url('image/view') . '?path=' . $path);
                } else if (in_array($mimetype, $this->_text)) {
                    if(filesize($filename) > $this->_size) {
                        return $this->error('文件过大，请下载后进行查看！');
                    } else {
                        $content = file_get_contents($filename);
                        return $this->success(2, '', $content);
                    }
                } else {
                    return $this->error('此文件不支持预览！'); 
                }
            } else {
                return $this->error('文件不存在');
            }     
        } else {
            return $this->error('非法访问！');
        }
    }
}
